package com.yhhvip.utils;
import java.io.UnsupportedEncodingException;
import sun.misc.*;
import java.io.UnsupportedEncodingException;
public class Base64Utils {
        /**
         * base64编码
         * @param
         * @return 编码后的字符串
         */
        public static String encode(String str) {
            byte[] b = null;
            String s = null;
            try {
                b = str.getBytes("utf-8");  //获取byte编码
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
            if (b != null) {
                s = new BASE64Encoder().encode(b);  //对字符串进行编码
            }
            return s;
        }

        /**
         * base64解码
         * @param
         * @return 解码后的字符串
         */
        public static String decode(String s) {
            byte[] b = null;
            String result = null;
            if (s != null) {
                BASE64Decoder decoder = new BASE64Decoder();
                try {
                    b = decoder.decodeBuffer(s);
                    result = new String(b, "utf-8");    //对字符串进行解码
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            return result;
        }

        public static void main(String[] args) {
            String str = "清明上河图";

            System.out.println(encode(str));
            System.out.println(decode(encode(str)));
        }




}
